﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProcessPublishBag
{
    class Program
    {
        public static List<string> CityDllNameList = new List<string>
            {
                "BaoXian.Model.Common.dll",
                ".exe",
                "BiHu.BaoXian.Artificial.RB.CON.dll",
                "BiHu.BaoXian.Artificial.RB.SubService.dll",
                "BiHu.BaoXian.ArtificialCommon.dll",
                "BiHu.BaoXian.ArtificialSubmit.RB.Common.dll",
                "BiHu.BaoXian.ClassCommon.dll",
                "BiHu.BaoXian.ControlLibrary.dll",
                //"BiHuManBu.BaoXian.VerificationCode.dll",
                //"Cjwdev.WindowsApi.dll",
                //"FrameLib.Common.dll",
                //"HtmlAgilityPack.dll",
                //"ICSharpCode.SharpZipLib.dll",
                //"log4net.dll",
                //"LogRecordLib.dll",
                //"Newtonsoft.Json.dll",
                //"Qiniu.dll",
                //"renbao.cer"
            };

      public static List<string> AdapterServiceDll = new List<string>
            {
                "BaoXian.Model.Common.dll",
                "BiHu.BaoXian.ArtificialSubmit.RB.Common.dll",
                "BiHu.BaoXian.ClassCommon.dll",
                "BiHuManBu.BaoXian.AdapterService.dll",
                //"Cjwdev.WindowsApi.dll",
                //"HtmlAgilityPack.dll",
                //"ICSharpCode.SharpZipLib.dll",
                //"log4net.dll",
                //"Newtonsoft.Json.dll"
            };
       public static string  path = @"C:\Users\Administrator\Desktop\PublishBag";

        static void Main(string[] args)
        {
           

            if (System.IO.Directory.Exists(path))
            {
                //删除旧的发布包
                if(Directory.Exists(path + "\\发布包")) Directory.Delete(path + "\\发布包",true);
                string[] directorys = Directory.GetDirectories(path);
                foreach (string directory in directorys)
                {
                    //Console.WriteLine(directory);
                    string[] filePaths = Directory.GetFiles(directory);
                    if (!filePaths.Any()) continue;
                    //生成目标城市中文目录
                    var targetDirectory = GetMapDirectoryName(Path.GetFileName(directory));
                    var generateDirectroyPath = path + "\\发布包\\" + targetDirectory + "\\artificial";
                    if (generateDirectroyPath.Contains("Lib下的Dll"))
                        generateDirectroyPath = path + "\\发布包\\" + targetDirectory;
                    DirectoryInfo cityDirectory = Directory.CreateDirectory(generateDirectroyPath);
                    if (!Directory.Exists(generateDirectroyPath))
                        throw new Exception("创建指定城市" + Path.GetFileName(directory) + "的中文目录失败！");
                    //Console.WriteLine("创建" + cityDirectory.Name == "Lib下的Dll" ? "Lib下的Dll" : cityDirectory.Parent.Name +"\"目录成功");
                    var targetName = (cityDirectory.Name == "Lib下的Dll") ? "Lib下的Dll" : cityDirectory.Parent.Name;
                    Console.WriteLine("创建 " + targetName + " 目录成功");
                    //File.WriteAllText("log.txt", targetName)
                    //遍历城市目录
                    //分adapterServie目录和城市目录
                    foreach (var file in filePaths)
                    {
                        //Console.WriteLine(Path.GetFileName(file));
                        //File.AppendAllText("0.txt","\"" + Path.GetFileName(file) + "\"," + "\r\n");

                        if (CityDllNameList.Any(name => Path.GetFileName(file).Contains(name))
                            && !Path.GetFileName(directory).Contains("BiHuManBu.BaoXian.AdapterService"))
                        {
                            File.Copy(file, cityDirectory.FullName + "\\"+ Path.GetFileName(file));
                        }
                        else if (AdapterServiceDll.Any(name => Path.GetFileName(file).Contains(name)) && Path.GetFileName(directory).Contains("BiHuManBu.BaoXian.AdapterService"))
                        {
                            File.Copy(file, cityDirectory.FullName + "\\" + Path.GetFileName(file));
                        }
                    }
                }

                if (Directory.Exists(path + "\\发布包"))
                {
                    var cityDirectoryNames = Directory.GetDirectories(path + "\\发布包");
                    foreach (var cityDirectoryName in cityDirectoryNames)
                    {
                        if (!cityDirectoryName.Contains("Lib下的Dll"))
                        {
                            foreach (var file in Directory.GetFiles(path + "\\发布包\\Lib下的Dll"))
                            {
                                File.Copy(file, cityDirectoryName +"\\"+Path.GetFileName(file));
                            }
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("指定路径不存在发布包");
            }

            Console.WriteLine("ok");
            Console.Read();
        }


        /// <summary>
        /// 生成目标目录映射字典
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string GetMapDirectoryName(string key)
        {
            try
            {
                var dictory = new Dictionary<string, string>()
                {
                    { "BiHu.BaoXian.Artificial.GuangDongZhuanDai.RB","广东专代"},
                    { "BiHu.BaoXian.Artificial.HeNanCheShang.RB","河南车商"},
                    { "BiHu.BaoXian.Artificial.LiaoNingZhuanDai.RB","辽宁专代"},
                    { "BiHu.BaoXian.Artificial.ShanXi1CheShang.RB","山西车商"},
                    { "BiHuManBu.BaoXian.AdapterService","Lib下的Dll"}
                };
                return dictory[key];
            }
            catch (Exception)
            {
                throw new Exception("字典里不存在指定城市" + key + "对应关系");
            }


        }
    }
}
